<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\CityList;
use App\Models\Report;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ReportController extends Controller
{
    use HasResourceActions;
    private $name = '城市报道';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Report);
        $grid->model()->where('status', '>', 0)->orderBy('id', 'desc');

        $grid->id('ID')->sortable();
        $grid->cityInfo()->name('城市');
        $grid->title('标题');
        $grid->report_time('时间');
        $grid->status('状态')->display(function ($status) {
            return Report::statusArray($status);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Report::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->report_time('时间');
        $show->remark('简介');
        $show->status('状态')->unescape()->as(function ($status) {
            return Report::statusArray($status);
        });
        $show->desc('报道详情')->unescape()->as(function ($desc) {
            return $desc;
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Report);

        $cityList = CityList::getCityList();

        $form->select('city', '城市')->options($cityList)->rules('required');
        $form->text('title', '标题')->rules('required');
        $form->datetime('report_time', '时间')->default(date('Y-m-d H:i:s'))->rules('required');
        $form->text('remark', '简介')->rules('required');
        $form->editor('desc', '报道详情')->rules('required');
        $states = [
            'on' => ['value' => 20, 'text' => '发布', 'color' => 'success'],
            'off' => ['value' => 10, 'text' => '新增', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states)->default(10);

        $para = request()->route()->parameters();
        if (count($para) > 0) {
            $id = $para['report'];
            $report = Report::find($id);
            if ($report->status == 10) {
                $form->tools(function (Form\Tools $tools) use ($id) {
                    $url = route('report.confirm', ['id' => $id]);
                    $tools->add('<div class="btn-group pull-right" style="margin-right: 5px"><a class="btn btn-sm btn-danger" href = "' . $url . '"><i class="fa fa-trash"></i>发布</a></div>');
                });
            }
        }

        return $form;
    }

    public function confirm($id)
    {
        $report = Report::find($id);
        if ($report->status != 10) {
            admin_toastr('当前新闻已经发布了', 'error');
            return back();
        }
        $report->status = 20;
        if ($report->save()) {
            admin_toastr('确认成功', 'success');
        } else {
            admin_toastr('确认失败', 'error');
        }

        $url = route('report.index');
        return redirect($url);
    }
}
